/**
 * Created by rongyao on 2017/9/22.
 */
requirejs.config({
	baseUrl: "../js/lib",
	paths: {
		"jquery": "jquery-3.2.1",
		"swiper":"swiper.jquery",
		"cookie":"jquery.cookie",
		"countdown":"jquery.countdown"
	}
})

requirejs(['jquery','cookie'], function ($) {
	
	//鼠标悬停，显示购物车列表
	$(".nav_right").on("mouseenter",function () {
		$(".hid_goods").css("height",$(".goods_list").height());
	});
	//鼠标离开，消失
	$(".nav_right").on("mouseleave",function () {
		$(".hid_goods").css("height",0);
	});
	
	$.ajax({
		url: "../json/second.json",
		type: "get",
		async: true,
		success: function (res) {
			$(".nav_left>li:not([class*='hh'])").each(function (index,ele) {
				//动态创建li加a
				for (var i = 0; i < res[index].hotActivities.length; i++) {
					var $li = $("<li></li>");
					$li.html("<a href=''>"+res[index].hotActivities[i]+"</a>");
					$(ele).find("ul").eq(0).append($li);
				}
				for (var j = 0; j < res[index].goods.length; j++) {
					var $li = $("<li></li>");
					$li.html("<a href=''>"+res[index].goods[j]+"</a>");
					$(ele).find("ul").eq(1).append($li);
				}
				//鼠标移入 显示二级列表
				$(ele).on("mouseenter",function () {
					$(ele).find(".pop_up").css("display","block");
				})
				//鼠标移出二级列表消失
				$(ele).on("mouseleave",function () {
					$(ele).find(".pop_up").css("display","none");
				})
			})
			
		}
	})
	
	
	//动态添加内容
	//获取通过url地址传过来的值
	var xx = window.location.href.split("?");
	var arr = xx[1].split("&");
	var obj1={};
	for (var k = 0; k < arr.length; k++) {
		var arr1 = arr[k].split("=")
		obj1[arr1[0]]=arr1[1]
	}
	var username = obj1.username;
	
	
	console.log(obj1);
	$.ajax({
		url:"../json/imgsrc.json",
		type:"post",
		dataType:"json",
		success:function (res) {
			//动态添加图片内容
			for (var s = 0; s < res[obj1.key][Number(obj1.id)].listbig.length; s++) {
				$(".swiper-wrapper").append("<div class='swiper-slide'><a href=''><img alt='图片无法加载'></a></div>");
			}
			$(".swiper-slide").each(function (index,ele) {
				$(this).find("img").attr("src",res[obj1.key][Number(obj1.id)].listbig[index]);
			});
			for (var s = 0; s < res[obj1.key][Number(obj1.id)].listbig.length; s++) {
				$(".wrap_left ul").append("<li><img alt=''></li>");
				$(".showImg").append("<img src="+res[obj1.key][Number(obj1.id)].listbig[s]+" alt=''>");
			}
			$(".wrap_left ul li").each(function (index,ele) {
				$(this).find("img").attr("src",res[obj1.key][Number(obj1.id)].listbig[index]);
			})
			//动态添加商品信息
			$("#silo a").text(res[obj1.key][Number(obj1.id)].a);
			$("#event a").text(res[obj1.key][Number(obj1.id)].brand);
			$("#product").text(res[obj1.key][Number(obj1.id)].tip);
			$(".product_name h1").text(res[obj1.key][Number(obj1.id)].tip);
			$("#productRMB").text(res[obj1.key][Number(obj1.id)].price);
			$(".color").find("img").attr("src",res[obj1.key][Number(obj1.id)].listbig[0]);
			$(".color p").text(res[obj1.key][Number(obj1.id)].color);
			$(".product_title").text(res[obj1.key][Number(obj1.id)].brand);
		}
	})
	
	//轮播图 使用swiper插件
	requirejs(['swiper'],function () {
		var mySwiper = new Swiper('.swiper-container', {
			prevButton:'.swiper-button-prev',
			nextButton:'.swiper-button-next',
			loop: true,
			speed:0
		})
		//鼠标选中小图，上面显示对应的大图 tab切换
		$(".wrap_left ul li").on("click",function () {
			var i = $(this).index();
			mySwiper = new Swiper('.swiper-container', {
				prevButton:'.swiper-button-prev',
				nextButton:'.swiper-button-next',
				loop: true,
				speed:0,
				initialSlide :i
			})
		})
	})
	
	//商品数量 加一 减一
	$(".add").on("click",function () {
		var num = parseInt($(this).prev("span").text());
		$(this).prev("span").text(num+1);
	})
	$(".dis").on("click",function () {
		var num = parseInt($(this).siblings("span").text());
		if(num-1<=1){
			num=2;
		}
		$(this).siblings("span").text(num-1);
	})
	
	//倒计时
	
	
	var ftTime = new Date("2017-10-5 00:00:00");
	
	var timer = setInterval(function () {
		var nowTime = new Date();
		var time = Date.parse(ftTime) - Date.parse(nowTime);
		var ms = time % 1000;
		var ss = parseInt(time / 1000) % 60;
		var min = parseInt(time / 1000 / 60) % 60;
		var hours = parseInt(time / 1000 / 60 / 60) % 24;
		var days = parseInt(time / 1000 / 60 / 60 / 24);
		
		days < 10 ? $(".day").text("0" + days) : $(".day").text(days);
		hours < 10 ? $(".hours").text("0" + hours) : $(".hours").text(hours);
		min < 10 ? $(".minite").text("0" + min) : $(".minite").text(min);
		ss < 10 ? $(".second").text("0" + ss) : $(".second").text(ss);
	})
	
	
	//点击加入购物袋
	console.log(username);
	if(!username||username=="undefined"){
		//点击logo 跳转主页面
		$("#logo a").on("click",function () {
			window.location.href = "index.html";
		})
		//如果当前账户没有登录 点击跳转到登陆界面
		$(".addCar>a>span").on("click",function () {
			window.location.href="login.html";
		})
		$(".nav_right").on("click",function () {
			window.location.href="login.html";
		})
	}else{
		//点击logo 跳转主页面
		$("#logo a").on("click",function () {
			window.location.href = "index.html?username="+username+"";
		})
		$(".nav_right span").on("click",function () {
			window.location.href="shopCar.html?username="+username+"";
		})
		$(".topLeft").html("<li></li><li class='user'><a href=''>"+username+"</a></li><li class='tui'><a>退出</a></li><li>|</li><li><a href=''>收藏</a></li>");
		//点击退出
		$(".tui").on("click",function () {
			requirejs(['cookie'],function () {
				$.removeCookie("users");
			})
			window.location.href = "goods.html?id="+obj1.id+"&key="+obj1.key+"";
		})
		$(".addCar>a>span").on("click",function () {
			$.ajax({
				url:"http://localhost/hz1706/mei/shopCarin.php",
				dataType:"json",
				type:"post",
				data:{
					username:username,
					dates:{
						goodsname:$(".product_title").text(),
						count:$(".count span").text(),
						price:$("#productRMB").text(),
						src:$(".color img").attr("src")
					}
				},
				success:function (res) {
					console.log(res);
					//重新加载 将内容先清空
					$(".goods_list").html('');
					if(res.data[0].shopGoods){
						var arr = JSON.parse(res.data[0].shopGoods);
						for (var i = 0; i < arr.length; i++) {
							$(".goods_list").append("<dl class='clear'><dt><img src="+arr[i].src+" alt=''></dt><dd><p><a href=''>"+arr[i].goodsname+"</a><span>"+arr[i].count+"*"+arr[i].price+"</span></p><a class='remove'>删除</a></dd></dl>");
						}
					}
					
					//右边购物车里边显示商品数量和总价格
					priceSum();
					
					$(".hid_goods").css("height",$(".goods_list").height());
					setTimeout(function () {
						$(".hid_goods").css("height",0);
					},1000);
				}
			})
			// $(".color img").attr("src")
			// $(".product_title").text()
			// $(".product_title").text()
			// $(".count span").text()
			// $("#productRMB").text()
			
		})
		
		//购物车init
		$.ajax({
			url:"http://localhost/hz1706/mei/init.php",
			dataType:"json",
			type:"post",
			data:{
				username:username
			},
			success:function (res) {
				console.log(res);
				$(".goods_list").html('');
				if(res.data[0].shopGoods){
					var arr = JSON.parse(res.data[0].shopGoods);
					for (var i = 0; i < arr.length; i++) {
						$(".goods_list").append("<dl class='clear'><dt><img src="+arr[i].src+" alt=''></dt><dd><p><a href=''>"+arr[i].goodsname+"</a><span>"+arr[i].count+"*"+arr[i].price+"</span></p><a class='remove'>删除</a></dd></dl>");
					}
				}
				
				//右边购物车里边显示商品数量和总价格
				priceSum();
			}
		})
		
		function priceSum() {
			$(".shopping_quantity span").eq(0).text($("dl").length);
			var sum = 0;
			$("dl").each(function (index, ele) {
				var str = $(ele).find("span").text();
				var arr = str.split("*");
				var price=arr[1];
				if(arr[1].indexOf(",")!=-1){
					var arr1 = arr[1].split(",");
					price = arr1[0]+arr1[1];
				}
				sum+=Number(arr[0])*Number(price);
			})
			$(".shopping_amount").text("¥"+sum+".00");
		}
		
		//点击删除 删除数据 以及购物车里的相应内容
		$(".goods_list").on("click",".remove",function () {
			var self = this;
			$.ajax({
				url:"http://localhost/hz1706/mei/shopCarout.php",
				dataType:"json",
				data:{
					username:username,
					goodsname:$(self).siblings("p").children("a").text()
				},
				type:"post",
				success:function (res) {
					$(self).parents("dl").remove();
					//右边购物车里边显示商品数量和总价格
					priceSum();
				}
			})
			
		})
	}
	
	
	
	
	
	
	//将导航条的高度记下 方便后面判断nav是否要固定顶部
	var topY = $("#nav").offset().top;
	//记下main的marginTop值 滚轮往下滚 margin-top值减小 知道0
	var marginT = $("#main").css("marginTop");
	
	var nowMargin;
	$(window).on("scroll",function () {
		//滚轮往下滚 margin-top值减小
		nowMargin = parseInt(marginT)-$(document).scrollTop()*1.5;
		if(nowMargin<=0){
			nowMargin=0;
		}
		$("#main").css("marginTop",nowMargin);
		
		//导航条固定顶部判断
		if($(document).scrollTop()>=topY){
			//scrollTop值大于nav的offsetTop 将nav固定在顶部
			$("#logo").css("marginBottom","42px");//原先位置用margin代替
			$("#nav").css({
				"position":"fixed",
				"top":0,
				"width":"100%"
			});
		}else{
			//否则回到原处
			$("#logo").css("marginBottom","0px");
			$("#nav").css({
				"position":"static"
			});
		}
	})
	
})